<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\MallAppear;

/**
 * MallAppearSearch represents the model behind the search form of `backend\models\MallAppear`.
 */
class MallAppearSearch extends MallAppear
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'openid', 'uid', 'status', 'create_time', 'finish_time', 'check_id', 'mid'], 'integer'],
            [['amount'], 'number'],
            [['reason'], 'safe'],
            [['create_time_start', 'create_time_end'], 'string'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = MallAppear::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'attributes' => [
                    'id',
                    'amount',
                    'create_time',
                    'finish_time',
                ],
                'defaultOrder' => ['create_time' => SORT_DESC]
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'openid' => $this->openid,
            'uid' => $this->uid,
            'mid' => $this->mid,
            'amount' => $this->amount,
            'status' => $this->status,
            //'create_time' => $this->create_time,
            'finish_time' => $this->finish_time,
            'check_id' => $this->check_id,
        ]);

        if ($this->create_time_start != '' && $this->create_time_end == '') {
            $query->andFilterWhere(['>=', 'create_time', strtotime($this->create_time_start)]);
        }

        if ($this->create_time_start == '' && $this->create_time_end != '') {
            $query->andFilterWhere(['<=', 'create_time', strtotime($this->create_time_end)]);
        }


        if ($this->create_time_start != '' && $this->create_time_end != '') {
            $query->andFilterWhere(['between', 'create_time', strtotime($this->create_time_start), strtotime($this->create_time_end) + 24 * 60 * 60]);
        }

        $query->andFilterWhere(['like', 'reason', $this->reason]);

        return $dataProvider;
    }
}
